package com.sscaipiao.android.activity.Base;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.Base.NormalActivity;
import com.sscaipiao.android.utils.CaipiaoServiceHelper;
import com.sscaipiao.android.utils.MathUtil;
import com.sscaipiao.android.utils.ShakeListener;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.widget.BallBase;
import com.sscaipiao.android.widget.BlueBall;
import com.sscaipiao.android.widget.RedBall;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Airay.Rong on 14-1-2.
 */
public class RedBallSingleArrayActivity extends NormalActivity implements BallBase.onClickCallback {
	private List<Integer> redBallIds = new ArrayList<Integer>();
	private int nRandomRedballNum;
	private int nMaxRedballNum;
	private int arRedBallID[] = {
			R.id.red_ball_01, R.id.red_ball_02, R.id.red_ball_03, R.id.red_ball_04, R.id.red_ball_05,
			R.id.red_ball_06, R.id.red_ball_07, R.id.red_ball_08, R.id.red_ball_09, R.id.red_ball_10,
			R.id.red_ball_11, R.id.red_ball_12, R.id.red_ball_13, R.id.red_ball_14, R.id.red_ball_15,
			R.id.red_ball_16, R.id.red_ball_17, R.id.red_ball_18, R.id.red_ball_19, R.id.red_ball_20,
			R.id.red_ball_21, R.id.red_ball_22, R.id.red_ball_23, R.id.red_ball_24, R.id.red_ball_25,
			R.id.red_ball_26, R.id.red_ball_27, R.id.red_ball_28, R.id.red_ball_29, R.id.red_ball_30,
			R.id.red_ball_31, R.id.red_ball_32, R.id.red_ball_33, R.id.red_ball_34, R.id.red_ball_35,
			R.id.red_ball_36,
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addShakeListener();
	}
	public void addRedBalls(int nMaxID) {
		if (nMaxID > arRedBallID.length) {
			return;
		}
		nMaxRedballNum = nMaxID;
		for (int i = 0; i < nMaxID; i++) {
			redBallIds.add(arRedBallID[i]);
			((RedBall) findViewById(arRedBallID[i])).setCallback(this);
		}
		for (int i = nMaxID; i < arRedBallID.length ; i++) {
			((RedBall) findViewById(arRedBallID[i])).setVisibility(View.INVISIBLE);
		}
	}

	public BallBase getRedBallByIndex(int index) {
		return  (BallBase)findViewById(redBallIds.get(index));
	}

	public void addShakeListener() {
		ShakeListener shakeListener = new ShakeListener(this);
		shakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
			public void onShake() {
				randomChooseBalls();
			}
		});
	}

	public void setRadomBallNum(int nRedNum, int nBlueNum) {
		nRandomRedballNum = nRedNum;
	}
	public void setRadomBallNum(int nRedNum) {
		nRandomRedballNum = nRedNum;
	}
	public void randomChooseBalls() {
		if (redBallIds.size() != 0) {
			randomChooseRedBalls();
		}
	}
	public void randomChooseRedBalls() {
		if (nRandomRedballNum == 0) {
			return;
		}
		resetAllRedBalls();
		Integer[] randomRedBalls = StringUtil.getRandom(redBallIds.size(), nRandomRedballNum);
		for (int num : randomRedBalls) {
			RedBall Ball = (RedBall) (findViewById(redBallIds.get(num - 1)));
			Ball.setSelected(true);
		}
		reCalculatorSum();
	}

	public void resetAllRedBalls() {
		for (int i = 0; i < redBallIds.size(); i++) {
			RedBall ball = (RedBall) findViewById(redBallIds.get(i));
			ball.reset();
		}
	}

	public void reCalculatorSum() {
		String sAgeFormat1 = getResources().getString(R.string.double_color_ball_hint);
		int notecount = notecount();
		setMoney(String.format(sAgeFormat1, notecount + "", (notecount * 2) + ""));
	}
	public int notecount() {
		int redNum = queryChoseRedBallNum();
		if (redNum < nRandomRedballNum) {
			return 0;
		}  else if (redNum == nRandomRedballNum) {
			return 1;
		} else {
			int redLottNum = (int) MathUtil.calcCNM(redNum, nRandomRedballNum);
			return redLottNum ;
		}
	}

	@Override
	public void onBallClick(View ball) {
		int choosedNum =   queryChoseRedBallNum();
		if (choosedNum > 20) {
			Toast.makeText(this, "你选择的红球数已经超过20个，不能再选",Toast.LENGTH_SHORT).show();
			BallBase curBall = (BallBase) ball;
			curBall.reset();
			return;
		}
		reCalculatorSum();
	}
	private void resetAllBalls() {
		if (redBallIds.size() != 0) {
			resetAllRedBalls();
		}
	}
	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (R.id.btn_radom == v.getId()) {
			if (redBallIds.size() != 0) {
				randomChooseRedBalls();
			}
		} else if (R.id.im_undo == v.getId()) {
			resetAllBalls();
			reCalculatorSum();
		} else if (R.id.btn_confirm == v.getId()) {
			//doConfirm();
		}
	}
	public int queryChoseRedBallNum() {
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < redBallIds.size(); i++) {
			ball = getRedBallByIndex(i);
			if (ball.isSelect()) {
				nBallNum ++;
			}
		}
		return nBallNum;
	}
	public List<Integer> queryChoseRedBallArray() {
		List<Integer> ChooseBalls = new ArrayList<Integer>();
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < redBallIds.size(); i++) {
			ball = (BallBase) findViewById(redBallIds.get(i));
			if (ball.isSelect()) {
				ChooseBalls.add(i+1);
			}
		}
		return ChooseBalls;
	}
	private List<String> queryChoseRedBallArray_Name() {
		List<String> ChooseBalls = new ArrayList<String>();
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < redBallIds.size(); i++) {
			ball = (BallBase) findViewById(redBallIds.get(i));
			if (ball.isSelect()) {
			    ChooseBalls.add(ball.getBallText());
			}
		}
		return ChooseBalls;
	}
	public void hideLottLostValue() {
		for (int i = 0; i < redBallIds.size() ; i++) {
			BallBase bb = (BallBase) findViewById(redBallIds.get(i));
			if (bb != null) {
				bb.setLabel("");
			}
		}
	}
	public void setupLostValues() {
		int redBallNum = redBallIds.size();
		for (int i = 0; i < redBallIds.size(); i++) {
			getRedBallByIndex(i).setLabel("");
		}
		for (int i = 0; i < lostValues.size(); i++) {
			String  text = lostValues.get(i);
			if (i < redBallNum) {
				getRedBallByIndex(i).setLabel(text);
			}
		}
	}
	/**
	 * @deprecated Use {@link #setupLottLostValues}
	 */
	@Deprecated
	public boolean getLottLostValues() {
		try {
			lostValues = CaipiaoServiceHelper.getLottLostValues_SingleRedBall(this, user, mLottCode);
			return true;
		} catch (Exception e) {

		}
		return false;
	}

	public void setupLottLostValues() {
		Thread thr1 = new Thread(mTask_getLottLostValues, "10");
		thr1.start();
	}

	Runnable mTask_getLottLostValues = new Runnable() {
		public void run() {
			try {
				lostValues = CaipiaoServiceHelper.getLottLostValues_SingleRedBall(RedBallSingleArrayActivity.this,
						user, mLottCode);
				if (lostValues.size() != 0) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							setupLostValues();
						}
					});
				}
			} catch (Exception e) {

			}
     		closeProgressDailog();
		}
	};
}